import my_base_sk.MySK as my_sk
from my_base_sk.MyEmbedding import embedding_gen
from semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPlugin
from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory
from semantic_kernel.memory.volatile_memory_store import VolatileMemoryStore

# 创建一个（内存）向量数据库
memory = SemanticTextMemory(storage=VolatileMemoryStore(), embeddings_generator=embedding_gen)

# 添加一个连接向量数据库的 Plugin
my_sk.kernel.add_plugin(TextMemoryPlugin(memory), "TextMemoryPlugin")

from semantic_kernel.text import split_markdown_lines

# 读取文件内容
with open('ChatALL.md', 'r') as f:
    # with open('sk_samples/SamplePlugin/SamplePlugin.py', 'r') as f:
    content = f.read()

# 将文件内容分片，单片最大 100 token（注意：SK 的 text split 功能目前对中文支持不如对英文支持得好）
lines = split_markdown_lines(content, 100)

collection_id = "generic"

# 将分片后的内容，存入内存
for index, line in enumerate(lines):
    await memory.save_information(collection=collection_id, id=index, text=line)

result = await memory.search(collection_id, "ChatALL怎么下载？")
print(result[0].text)
